#    This file is part of SRIS.
#
#    SRIS is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    SRIS is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with SRIS.  If not, see <http://www.gnu.org/licenses/>.
#
#    Copyright 2011 David Irvine
#

from django.db import models
import datetime
class Scanner(models.Model):
	name=models.CharField(
			max_length=128,
			help_text="Provide a unique name for this scanner",
			verbose_name="Scanner",
			)
	def __unicode__(self):
		return self.name

class FileSystem(models.Model):
	URL=models.CharField(
			max_length=512,
			verbose_name="URL",
			help_text="URL to file system",
			)

	scanner=models.ForeignKey(
			'Scanner',
			related_name='filesystems',
			verbose_name='Scanner',
			help_text="Associated file systems",
			)

	def __unicode__(self):
		return self.URL


class Scan(models.Model):
	def __unicode__(self):
		return self.FileSystem.__unicode__()

	ts=models.DateTimeField(
		verbose_name='Time Stamp',
		help_text="""The datetime object when the scan was uploaded""",
		auto_now_add=True,
		)

	state=models.CharField(
			max_length=12,
			verbose_name='State',
			help_text="The current state of the scan, workflow is: SCANNING, SCANNED, PROCESSING, PROCESSED",
			default="SCANNING",
			)

	filesScanned=models.BigIntegerField(
			default=0,
			verbose_name='Files Scanned',
			help_text='The number of files successfully read by the scanner',
			)

	filesUnread=models.BigIntegerField(
			default=0,
			verbose_name='Unreadable Files',
			help_text='The number of files not read by the scanner',
			)

	vfsSize=models.BigIntegerField(
			default=0,
			verbose_name="VFS Size (Bytes)",
			help_text="The size of the filesystem in bytes",
			)

	vfsFree=models.BigIntegerField(
			default=0,
			verbose_name="VFS Size (Bytes)",
			help_text="The size of the filesystem in bytes",
			)

	vfsFilesListed=models.BigIntegerField(
			default=0,
			verbose_name="VFS Free (Bytes)",
			help_text="The free space reported by the filesystem in bytes",
			)

	FileSystem=models.ForeignKey(
			'FileSystem',
			related_name='scans',
			verbose_name='File System',
			help_text="Scans associated with this file system",
			)

	processedBy=models.CharField(
			max_length=128,
			help_text="The unique id of the processor that is processing this scan",
			verbose_name="Processor ID",
			)


	def vfsSizeG(self):
		return self.vfsSize/1024/1024/1024



class VFSExtendedInfo(models.Model):
	name=models.CharField(
			max_length=128,
			verbose_name='Name',
			help_text="The name of the optional VFS information supplied",
			)
	value=models.CharField(
			verbose_name='Value',
			max_length=128,
			help_text="The value of the optional VFS information supplied",
			)
	Scan=models.ForeignKey(
		'Scan',
		related_name='VFSExtendedInfo',
		)
class DataFile(models.Model):
	fileName=models.CharField(
			max_length=2048,
			verbose_name="Data File",
			help_text="The relative path to the file on disk",
			)
	Scan=models.ForeignKey(
			'Scan',
			related_name='dataFiles',
			verbose_name='Files',
			help_text="Filesnames of chunks",
	)


class Bucket(models.Model):
	Scan=models.ForeignKey(
		'Scan',
		related_name='Buckets',
		verbose_name='Scan',
		help_text="The scan associated with this bucket",
	)

	globalRoot=models.CharField(
			max_length=256,
			verbose_name="Root",
			help_text="The globally identifiable root for the filesystem",
			)
	userName=models.CharField(
			max_length=30,
			verbose_name='User Name',
			help_text='The user name of the user as returned by getent',
			)

	userId=models.BigIntegerField(
			default=0,
			verbose_name='User ID',
			help_text='The numerical ID of the user owning the file',
	)

	groupId=models.BigIntegerField(
			default=0,
			verbose_name='Group ID',
			help_text='The numerical ID of the group owning the file',
			)

	groupName=models.CharField(
			max_length=30,
			verbose_name="Group Name",
			help_text="The name of the group owning the file",
			)

	aTime=models.CharField(
			max_length=128,
			help_text="The access time range of the file",
			verbose_name="Access Time",
			)

	mTime=models.CharField(
			max_length=128,
			help_text="The modification time range of the file",
			verbose_name="Modification Time",
			)
	
	cTime=models.CharField(
			max_length=128,
			help_text="The creation time range of the file",
			verbose_name="Creation Time",
			)

	size=models.CharField(
			max_length=128,
			help_text="The size range of the file",
			verbose_name="Size Range",
			)

	docType=models.CharField(
			max_length=128,
			help_text="The type of document",
			verbose_name="Document Type",
			)

	totalSize=models.BigIntegerField(
			default=0,
			verbose_name="Total Size",
			help_text="The size in bytes of the data in the bucket",
			)

	totalFiles=models.BigIntegerField(
			default=0,
			verbose_name="Total Files",
			help_text="The total number of files in the bucket",
			)



class ScannedFile(models.Model):
	def __unicode__(self):
		return self.globalPath

	Bucket=models.ForeignKey(
			'Bucket',
			related_name='Files',
			verbose_name="Bucket",
			help_text="The bucket this file belongs to",
			)

	globalPath=models.CharField(
			max_length=2048,
			help_text="The globally unique path to the file",
			verbose_name="Global Path",
			)

	LocalPath=models.CharField(
			max_length=2048,
			help_text="The local path to the file",
			verbose_name="Local Path",
			)

	uid=models.BigIntegerField(
			default=0,
			verbose_name="User ID",
			help_text="The User ID owning the file",
			)

	gid=models.BigIntegerField(
			default=0,
			verbose_name="Group ID",
			help_text="The Group ID owning the file",
			)

	groupName=models.CharField(
			max_length=30,
			verbose_name="Group Name",
			help_text="The name of the group owning the file",
			)

	userName=models.CharField(
			max_length=30,
			verbose_name='User Name',
			help_text='The user name of the user as returned by getent',
			)

	size=models.BigIntegerField(
			default=0,
			verbose_name="Size",
			help_text="The Size of the file in bytes",
			)

	atime=models.BigIntegerField(
			default=0,
			verbose_name="Access Time",
			help_text="The access time of the file in seconds since the epoch",
			)

	mtime=models.BigIntegerField(
			default=0,
			verbose_name="Modification Time",
			help_text="The Modification time of the file in seconds since the epoch",
			)

	ctime=models.BigIntegerField(
			default=0,
			verbose_name="Creation Time",
			help_text="The creation time of the file in seconds since the epoch",
			)
	def accessTime(self):
		return datetime.datetime.fromtimestamp(self.atime)
	def modificationTime(self):
		return datetime.datetime.fromtimestamp(self.mtime)
	def creationTime(self):
		return datetime.datetime.fromtimestamp(self.ctime)
